﻿Imports System.Math
Imports System.DBNull
Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion

Partial Class CustoDeImportacaoDespAduaneiras
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Faz com que ao sair do aplicativo, limpe o buffer e não podendo entrar na página através do butão voltar do Browser.
        Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)
        'Fim

        Response.Cache.SetExpires(DateTime.Now.AddSeconds(1))

        If Not Page.IsPostBack Then

            Dim strConnection As String
            strConnection = "A"

            lblModulo.Text = "Operações"
            lblModuloItem.Text = "Dados da Importação - Despesas Aduaneiras"

            If (LoadHolding(strConnection) = True) Then

                FillDDL()

                Try
                    If (Session("strOperacao") = "I") Then

                    ElseIf (Session("strOperacao") = "C" Or Session("strOperacao") = "A" Or Session("strOperacao") = "E") Then ' Consultar | Alterar | Excluir
                        LoadData()
                        If IsNumeric(txtVlTaxaTSA.Text) = True Then
                            If IsNumeric(txtVlTotDespesas.Text) = True Then
                                txtBCICMS.Text = Round(CDbl((+txtVlTotDespesas.Text + txtVlTaxaTSA.Text) / 0.82999999999999996), 2)
                                If (ddlTipoTributacaoICMS.SelectedValue = "1") Then
                                    txtVLICMS.Text = Round(CDbl((+txtBCICMS.Text) * 0.17000000000000001), 2)
                                Else
                                    txtVLICMS.Text = 0
                                End If
                            End If
                        Else
                            txtVLICMS.Text = ""
                        End If
                    End If
                    Dim ConnectionTest As Boolean
                    If Not (EditRecord(ConnectionTest) = True) Then
                        Exit Sub
                    End If

                    If (Session("strOperacao") = "C") Then ' Consultar
                        btnGravar.Enabled = False
                    End If

                Catch ex As Exception
                    lblMsg.Text = ex.Message
                End Try

            End If

        End If

    End Sub

    ' Step 02
    Private Function LoadHolding(ByVal strConn As String) As Boolean

        ' CAMG Informática | Marcelo Barcelos | 26.06.2010 08h


        LoadHolding = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim theQueryString As String = "SELECT CodigoDaHolding, NomeDaHolding" & _
                         " FROM Holding;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                If (theSqlDataReader.Read() = True) Then
                    lblHolding.Text = theSqlDataReader(1)
                    lblHolding.ToolTip = theSqlDataReader(0)
                    LoadHolding = True
                Else
                    lblMsg.Text = "Holding não cadastrada no Sistema."
                End If
                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using


    End Function

    '' Step 03
    Private Sub FillDDL()

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodigoDaCia, NomeDaCia" & _
                     " FROM Companhia" & _
                     " WHERE CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
                     " ORDER BY CodigoDaCia;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                ddlCompanhia.DataTextField = "NomeDaCia"
                ddlCompanhia.DataValueField = "CodigoDaCia"
                ddlCompanhia.DataSource = mydatatable
                ddlCompanhia.DataBind()

                ddlCompanhia.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using


        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodFornecedorServico, RazaoSocialFornecedorServico AS FornecedorServico" & _
                         " FROM fornecedorservico WHERE CodTipoFornecedorServico = 2" & _
                         " ORDER BY RazaoSocialFornecedorServico;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)

                ddlFornecedorServico.DataTextField = "FornecedorServico"
                ddlFornecedorServico.DataValueField = "CodFornecedorServico"
                ddlFornecedorServico.DataSource = mydatatable
                ddlFornecedorServico.DataBind()

                ddlFornecedorServico.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using


        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodigoDaMoeda, SiglaDaMoeda + ' - ' + NomeDaMoeda AS Moeda" & _
                 " FROM Moeda" & _
                 " ORDER BY SiglaDaMoeda;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                ddlMoeda.DataTextField = "Moeda"
                ddlMoeda.DataValueField = "CodigoDaMoeda"
                ddlMoeda.DataSource = mydatatable
                ddlMoeda.DataBind()

                ddlMoeda.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodTipoTributacao, SiglaTipoTributacao + ' - ' + DscTipoTributacao AS TipoTributacao" & _
                         " FROM TipoTributacao" & _
                         " ORDER BY CodTipoTributacao;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                ddlTipoTributacaoII.DataTextField = "TipoTributacao"
                ddlTipoTributacaoII.DataValueField = "CodTipoTributacao"
                ddlTipoTributacaoII.DataSource = mydatatable
                ddlTipoTributacaoII.DataBind()

                ddlTipoTributacaoII.Items.Insert(0, "")

                ddlTipoTributacaoIPI.DataTextField = "TipoTributacao"
                ddlTipoTributacaoIPI.DataValueField = "CodTipoTributacao"
                ddlTipoTributacaoIPI.DataSource = mydatatable
                ddlTipoTributacaoIPI.DataBind()

                ddlTipoTributacaoIPI.Items.Insert(0, "")

                ddlTipoTributacaoPIS.DataTextField = "TipoTributacao"
                ddlTipoTributacaoPIS.DataValueField = "CodTipoTributacao"
                ddlTipoTributacaoPIS.DataSource = mydatatable
                ddlTipoTributacaoPIS.DataBind()

                ddlTipoTributacaoPIS.Items.Insert(0, "")

                ddlTipoTributacaoCOFINS.DataTextField = "TipoTributacao"
                ddlTipoTributacaoCOFINS.DataValueField = "CodTipoTributacao"
                ddlTipoTributacaoCOFINS.DataSource = mydatatable
                ddlTipoTributacaoCOFINS.DataBind()

                ddlTipoTributacaoCOFINS.Items.Insert(0, "")

                ddlTipoTributacaoICMS.DataTextField = "TipoTributacao"
                ddlTipoTributacaoICMS.DataValueField = "CodTipoTributacao"
                ddlTipoTributacaoICMS.DataSource = mydatatable
                ddlTipoTributacaoICMS.DataBind()

                ddlTipoTributacaoICMS.Items.Insert(0, "")

                ddlTipoTributacaoICMSST.DataTextField = "TipoTributacao"
                ddlTipoTributacaoICMSST.DataValueField = "CodTipoTributacao"
                ddlTipoTributacaoICMSST.DataSource = mydatatable
                ddlTipoTributacaoICMSST.DataBind()

                ddlTipoTributacaoICMSST.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    '' Step 04
    Private Sub LoadData()

        Dim intQtdeItems As Integer = 0

        ddlCompanhia.Enabled = False
        ddlEstabelecimento.Enabled = False
        ddlFornecedorServico.Enabled = False
        txtNumeroDocumento.Enabled = False
        ddlDI.Enabled = False
        txtDataDocumento.Enabled = False
        txtDataLancamento.Enabled = False
        txtDataRegistro.Enabled = False
        ddlMoeda.Enabled = False

        If (Session("strOperacao").ToString = "C" Or Session("strOperacao").ToString = "E") Then ' Consultar | Excluir


        End If

        ddlCompanhia.SelectedValue = Session("strCodigoDaCia").ToString
        FillDDLEstabelecimento()

        ddlEstabelecimento.SelectedValue = Session("strCodigoDoEstabelecimento").ToString
        LoadUFDestino()

        ddlFornecedorServico.SelectedValue = Session("strCodFornecedorServico").ToString
        LoadUFOrigem()

        txtNumeroDocumento.Text = Session("strNumeroDocumento").ToString
        txtDataDocumento.Text = Session("strDataDocumento").ToString

        'FillDDLDI()
        ddlDI.Text = Session("strDI").ToString

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)


            Dim theQueryString As String = "SELECT" & _
                     " DataLancamento, DataRegistro, CodMoeda, CodTipoTributacaoII," & _
                     " CodTipoTributacaoIPI, CodTipoTributacaoPIS, CodTipoTributacaoCOFINS, CodTipoTributacaoICMS, CodTipoTributacaoICMSST," & _
                     " VlTotDespesas, VlDescontoComercial, VlDebitos, VlOutrosCreditos, VlTaxaTSA, VlTaxaFTI, VlBCICMS, VLICMS, SISCOMEX, ValorTaxaConversao"
            theQueryString = theQueryString & _
                     " FROM custoimportacao" & _
                     " WHERE CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
                     " AND CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'" & _
                     " AND CodigoDoEstabelecimento = '" & ddlEstabelecimento.SelectedValue & "'" & _
                     " AND CodFornecedorServico = '" & ddlFornecedorServico.SelectedValue & "'" & _
                     " AND NumeroDocumento = '" & UCase(Trim(txtNumeroDocumento.Text)) & "';"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try

                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                Do While theSqlDataReader.Read()

                    If Not (IsDBNull(theSqlDataReader!DataLancamento) = True) Then
                        txtDataLancamento.Text = Format(CDate(theSqlDataReader!DataLancamento.ToString), "dd/MM/yyyy")
                    End If

                    If Not (IsDBNull(theSqlDataReader!DataRegistro) = True) Then
                        txtDataRegistro.Text = Format(CDate(theSqlDataReader!DataRegistro.ToString), "dd/MM/yyyy")
                    End If

                    If Not (IsDBNull(theSqlDataReader!CodMoeda) = True) Then
                        ddlMoeda.SelectedValue = theSqlDataReader!CodMoeda
                    End If

                    If Not (IsDBNull(theSqlDataReader!CodTipoTributacaoII) = True) Then ddlTipoTributacaoII.SelectedValue = theSqlDataReader!CodTipoTributacaoII
                    If Not (IsDBNull(theSqlDataReader!CodTipoTributacaoIPI) = True) Then ddlTipoTributacaoIPI.SelectedValue = theSqlDataReader!CodTipoTributacaoIPI
                    If Not (IsDBNull(theSqlDataReader!CodTipoTributacaoPIS) = True) Then ddlTipoTributacaoPIS.SelectedValue = theSqlDataReader!CodTipoTributacaoPIS
                    If Not (IsDBNull(theSqlDataReader!CodTipoTributacaoCOFINS) = True) Then ddlTipoTributacaoCOFINS.SelectedValue = theSqlDataReader!CodTipoTributacaoCOFINS
                    If Not (IsDBNull(theSqlDataReader!CodTipoTributacaoICMS) = True) Then ddlTipoTributacaoICMS.SelectedValue = theSqlDataReader!CodTipoTributacaoICMS
                    If Not (IsDBNull(theSqlDataReader!CodTipoTributacaoICMSST) = True) Then ddlTipoTributacaoICMSST.SelectedValue = theSqlDataReader!CodTipoTributacaoICMSST

                    If Not (IsDBNull(theSqlDataReader!VlTotDespesas) = True) Then
                        txtVlTotDespesas.Text = theSqlDataReader!VlTotDespesas
                    End If

                    If Not (IsDBNull(theSqlDataReader!VlDescontoComercial) = True) Then
                        txtVlDescontoComercial.Text = theSqlDataReader!VlDescontoComercial
                    End If

                    If Not (IsDBNull(theSqlDataReader!VlDebitos) = True) Then
                        txtVlDebitos.Text = theSqlDataReader!VlDebitos
                    End If

                    If Not (IsDBNull(theSqlDataReader!VlOutrosCreditos) = True) Then
                        txtVlOutrosCreditos.Text = theSqlDataReader!VlOutrosCreditos
                    End If

                    If Not (IsDBNull(theSqlDataReader!VlTaxaTSA) = True) Then
                        txtVlTaxaTSA.Text = theSqlDataReader!VlTaxaTSA
                    End If

                    If Not (IsDBNull(theSqlDataReader!VlTaxaFTI) = True) Then
                        txtVlTaxaFTI.Text = theSqlDataReader!VlTaxaFTI
                    End If

                    If Not (IsDBNull(theSqlDataReader!VlBCICMS) = True) Then
                        txtBCICMS.Text = theSqlDataReader!VlBCICMS
                    End If

                    If Not (IsDBNull(theSqlDataReader!VLICMS) = True) Then
                        txtVLICMS.Text = theSqlDataReader!VLICMS
                    End If

                    If Not (IsDBNull(theSqlDataReader!SISCOMEX) = True) Then
                        txtSISCOMEX.Text = theSqlDataReader!SISCOMEX
                    End If

                    If Not (IsDBNull(theSqlDataReader!ValorTaxaConversao) = True) Then
                        lblTaxaConversao.Text = theSqlDataReader!ValorTaxaConversao
                    End If

                Loop

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Private Sub LoadUFOrigem()


        lblUFOrigem.Text = ""

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)


            Dim theQueryString As String = "SELECT" & _
                     " A.CodigoDaUF," & _
                     " B.SiglaDaUF"

            theQueryString = theQueryString & _
                     " FROM FornecedorServico A, uf B" & _
                     " WHERE A.CodigoDaUF = B.CodigoDaUF AND A.CodFornecedorServico = '" & ddlFornecedorServico.SelectedValue & "';"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try

                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                Do While theSqlDataReader.Read()

                    If (IsDBNull(theSqlDataReader!SiglaDaUF) = True) Then
                        lblMsg.Text = "Despachante sem UF informada."
                    Else
                        lblUFOrigem.Text = theSqlDataReader!SiglaDaUF
                        lblUFOrigem.ToolTip = theSqlDataReader!CodigoDaUF
                    End If

                Loop

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Private Sub LoadUFDestino()

        lblUFDestino.Text = ""

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim theQueryString As String = "SELECT A.CodigoDaUF, B.SiglaDaUF"

            theQueryString = theQueryString & _
                     " FROM Estabelecimento A, uf B" & _
                     " WHERE A.CodigoDaUF = B.CodigoDaUF AND A.CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
                     " AND A.CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'" & _
                     " AND A.CodigoDoEstabelecimento = '" & ddlEstabelecimento.SelectedValue & "';"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try

                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                Do While theSqlDataReader.Read()

                    If Not (IsDBNull(theSqlDataReader!SiglaDaUF) = True) Then
                        lblUFDestino.Text = theSqlDataReader!SiglaDaUF
                        lblUFDestino.ToolTip = theSqlDataReader!CodigoDaUF
                    Else
                        lblMsg.Text = "Estabelecimento sem UF informada."
                    End If

                Loop

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using


    End Sub


    '' Step 05
    Protected Sub ddlCompanhia_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCompanhia.SelectedIndexChanged

        FillDDLEstabelecimento()

    End Sub

    '' Step 06
    Private Sub FillDDLEstabelecimento()

        ' CAMG Informática | Marcelo Barcelos | 05.01.2010 16h45

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodigoDoEstabelecimento, CodigoDoEstabelecimento + ' - ' + RazaoSocialDoEstabelecimento AS Estabelecimento" & _
                     " FROM Estabelecimento" & _
                     " WHERE CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
                     " AND CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'" & _
                     " ORDER BY CodigoDoEstabelecimento;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                ddlEstabelecimento.DataTextField = "Estabelecimento"
                ddlEstabelecimento.DataValueField = "CodigoDoEstabelecimento"
                ddlEstabelecimento.DataSource = mydatatable
                ddlEstabelecimento.DataBind()

                ddlEstabelecimento.Items.Insert(0, "")
                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    '' Step 07
    'Private Sub FillDDLDI()

    '    ' CAMG Informática | Marcelo Barcelos | 05.01.2010 16h45

    '    Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

    '        Dim mydatatable As New DataTable
    '        Dim theQueryString As String

    '        theQueryString = "SELECT DI" & _
    '                 " FROM DIHeader" & _
    '                 " WHERE CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
    '                 " AND CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'" & _
    '                 " AND CodigoDoEstabelecimento = '" & ddlEstabelecimento.SelectedValue & "'"

    '        If (Session("strOperacao") = "I") Then ' Incluir
    '            theQueryString = theQueryString & _
    '                     " AND CodSituacaoDI IN (1, 2)" & _
    '                     " AND DI NOT IN (SELECT DI FROM ControleDespesasHeader WHERE di is NOT NULL)"
    '        End If

    '        theQueryString = theQueryString & _
    '                 " ORDER BY DI;"

    '        Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
    '        theSqlCommand.CommandText = theQueryString

    '        Try
    '            theSqlConnection.Open()
    '            Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

    '            mydatatable.Load(theSqlDataReader)
    '            ddlDI.DataTextField = "DI"
    '            ddlDI.DataValueField = "DI"
    '            ddlDI.DataSource = mydatatable
    '            ddlDI.DataBind()

    '            ddlDI.Items.Insert(0, "")
    '            theSqlDataReader.Close()
    '        Catch ex As Exception
    '            'theSqlDataReader.Close()
    '            lblMsg.Text = "Msg DB: " & ex.Message
    '        End Try

    '    End Using

    'End Sub

    '' Step 08
    '' Step 09
    Protected Sub btnGravar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGravar.Click

        Dim ConnectionTest As String
        ConnectionTest = "A"

        lblMsg.Text = ""


        If (Session("strOperacao") = "I") Then ' Incluir

            If Not (DataValid() = True) Then
                Exit Sub
            End If

            If (CodeExist(ConnectionTest) = True) Then
                Exit Sub
            End If

            If Not (AddRecord(ConnectionTest) = True) Then
                Exit Sub
            End If

        ElseIf (Session("strOperacao") = "A") Then ' Alterar

            If Not (DataValid() = True) Then
                Exit Sub
            End If

            If Not (EditRecord(ConnectionTest) = True) Then
                Exit Sub
            End If

        ElseIf (Session("strOperacao") = "E") Then ' Excluir

            If (VerifyData(ConnectionTest) = True) Then
                Exit Sub
            End If

            If Not (DeleteRecord(ConnectionTest) = True) Then
                Exit Sub
            End If

        End If

        lblMsg.ForeColor = Drawing.Color.Green
        lblMsg.Text = "Operação realizada com sucesso. Clique no botão 'OK'"

        btnGravar.Enabled = False
        btnVoltar.Text = "OK"

    End Sub

    '' Step 10
    Private Function DataValid() As Boolean

        Dim strModeloAtual As String

        DataValid = False

        If (ddlCompanhia.Text = "") Then
            lblMsg.Text = "Companhia deve ser selecionada."
            Exit Function
        End If

        If (ddlEstabelecimento.Text = "") Then
            lblMsg.Text = "Estabelecimento deve ser selecionado."
            Exit Function
        End If

        If (ddlFornecedorServico.Text = "") Then
            lblMsg.Text = "Despachante deve ser selecionado."
            Exit Function
        End If

        If (Trim(txtNumeroDocumento.Text) = "") Then
            lblMsg.Text = "Número do Documento deve ser preenchido."
            Exit Function
        End If

        If (Trim(txtDataDocumento.Text) = "") Then
            lblMsg.Text = "Data do Documento deve ser preenchida."
            Exit Function
        End If

        If Not IsDate(txtDataDocumento.Text) = True Then
            lblMsg.Text = "Data do Documento inválida."
            Exit Function
        End If

        If (Trim(txtDataLancamento.Text) = "") Then
            lblMsg.Text = "Data de Lançamento deve ser preenchida."
            Exit Function
        End If

        If Not IsDate(txtDataLancamento.Text) = True Then
            lblMsg.Text = "Data de Lançamento inválida."
            Exit Function
        End If

        If (CDate(txtDataLancamento.Text) < txtDataDocumento.Text) Then
            lblMsg.Text = "Data de Lançamento deve ser igual ou posterior à Data do Documento."
            Exit Function
        End If

        If (ddlDI.Text = "") Then
            lblMsg.Text = "DI deve ser selecionada."
            Exit Function
        End If

        If (ddlTipoTributacaoII.Text = "") Then
            lblMsg.Text = "Tributação II (Modelo atual) deve ser selecionada."
            Exit Function
        End If

        If (ddlTipoTributacaoIPI.Text = "") Then
            lblMsg.Text = "Tributação IPI (Modelo atual) deve ser selecionada."
            Exit Function
        End If

        If (ddlTipoTributacaoPIS.Text = "") Then
            lblMsg.Text = "Tributação PIS (Modelo atual) deve ser selecionada."
            Exit Function
        End If

        If (ddlTipoTributacaoCOFINS.Text = "") Then
            lblMsg.Text = "Tributação COFINS (Modelo atual) deve ser selecionada."
            Exit Function
        End If

        If (ddlTipoTributacaoICMS.Text = "") Then
            lblMsg.Text = "Tributação ICMS (Modelo atual) deve ser selecionada."
            Exit Function
        End If

        If (ddlTipoTributacaoICMSST.Text = "") Then
            lblMsg.Text = "Tributação ICMS Substituto (Modelo atual) deve ser selecionada."
            Exit Function
        End If

        strModeloAtual = Left(ddlTipoTributacaoII.Text, 1) & Left(ddlTipoTributacaoIPI.Text, 1)
        strModeloAtual = strModeloAtual & Left(ddlTipoTributacaoPIS.Text, 1) & Left(ddlTipoTributacaoCOFINS.Text, 1)
        strModeloAtual = strModeloAtual & Left(ddlTipoTributacaoICMS.Text, 1) & Left(ddlTipoTributacaoICMSST.Text, 1)


        DataValid = True

    End Function

    '' Step 11
    Private Function CodeExist(ByVal strConn As String) As Boolean

        CodeExist = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim theQueryString As String
            theQueryString = "SELECT NumeroDocumento" & _
                     " FROM custoimportacao" & _
                     " WHERE CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
                     " AND CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'" & _
                     " AND CodigoDoEstabelecimento = '" & ddlEstabelecimento.SelectedValue & "'" & _
                     " AND CodFornecedorServico = '" & ddlFornecedorServico.SelectedValue & "'" & _
                     " AND NumeroDocumento = '" & UCase(Trim(txtNumeroDocumento.Text)) & "';"
            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                If (theSqlDataReader.Read()) Then
                    CodeExist = True
                    lblMsg.Text = "Número do Documento " & txtNumeroDocumento.Text & " já existe no Sistema."
                End If
                theSqlDataReader.Close()
            Catch ex As Exception
                CodeExist = True
                'theSqlDataReader.Connection.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Function

    '' Step 12
    '' Step 13
    Private Function AddRecord(ByVal strConn As String) As Boolean

        Dim strDataDocumento As String
        Dim strDataLancamento As String
        Dim strDataRegistro As String
        Dim nDiaCreated As String
        Dim nMesCreated As String
        Dim nAnoCreated As String
        Dim nLastUpdated As String

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            'Dim dAnoVigente As String
            'Dim i As Long

            Dim theQueryString As String = "SELECT CONVERT(CHAR(10), GETDATE(), 103) AS DATADOSERVIDOR"
            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString
            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()
                Do While theSqlDataReader.Read()
                    Session("dDTAno") = Trim(theSqlDataReader(0))
                Loop
                theSqlDataReader.Close()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try

        End Using

        nDiaCreated = Mid(Session("dDTAno"), 1, 2)
        nMesCreated = Mid(Session("dDTAno"), 4, 2)
        nAnoCreated = Mid(Session("dDTAno"), 7, 4)
        nLastUpdated = nDiaCreated & "/" & nMesCreated & "/" & nAnoCreated

        If (IsDate(nLastUpdated)) Then
            nLastUpdated = "'" & FormatMediumDate(nLastUpdated) & "'"
        Else
            nLastUpdated = "Null"
        End If

        AddRecord = False

        Dim strVlDescontoComercialUSD As String
        Dim strVlDebitosUSD As String
        Dim strVlOutrosCreditosUSD As String
        Dim strVlTaxaTSAUSD As String
        Dim strVlTaxaFTIUSD As String
        Dim strSISCOMEXUSD As String

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)


            strVlDescontoComercialUSD = Round(CDbl(txtVlDescontoComercial.Text / lblTaxaConversao.Text), 2)
            strVlDescontoComercialUSD = Replace(strVlDescontoComercialUSD, ",", ".")

            strVlDebitosUSD = Round(CDbl(txtVlDebitos.Text / lblTaxaConversao.Text), 2)
            strVlDebitosUSD = Replace(strVlDebitosUSD, ",", ".")

            strVlOutrosCreditosUSD = Round(CDbl(txtVlOutrosCreditos.Text / lblTaxaConversao.Text), 2)
            strVlOutrosCreditosUSD = Replace(strVlOutrosCreditosUSD, ",", ".")

            strVlTaxaTSAUSD = Round(CDbl(txtVlTaxaTSA.Text / lblTaxaConversao.Text), 2)
            strVlTaxaTSAUSD = Replace(strVlTaxaTSAUSD, ",", ".")

            strVlTaxaFTIUSD = Round(CDbl(txtVlTaxaFTI.Text / lblTaxaConversao.Text), 2)
            strVlTaxaFTIUSD = Replace(strVlTaxaFTIUSD, ",", ".")

            strSISCOMEXUSD = Round(CDbl(txtSISCOMEX.Text / lblTaxaConversao.Text), 2)
            strSISCOMEXUSD = Replace(strSISCOMEXUSD, ",", ".")


            strDataDocumento = "'" & FormatMediumDate(txtDataDocumento.Text) & "'"
            strDataLancamento = "'" & FormatMediumDate(txtDataLancamento.Text) & "'"
            strDataRegistro = "'" & FormatMediumDate(txtDataRegistro.Text) & "'"


            Dim theQueryString As String
            theQueryString = "INSERT INTO custoimportacao" & _
                     " (" & _
                     " CodigoDaHolding," & _
                     " CodigoDaCia," & _
                     " CodigoDoEstabelecimento," & _
                     " CodFornecedorServico," & _
                     " NumeroDocumento," & _
                     " DataDocumento," & _
                     " DataLancamento," & _
                     " DI," & _
                     " DataRegistro," & _
                     " CodMoeda," & _
                     " CreateUser," & _
                     " Created" & _
                     " )"

            theQueryString = theQueryString & _
                     " VALUES " & _
                     " (" & _
                     " '" & lblHolding.ToolTip & "'," & _
                     " '" & ddlCompanhia.SelectedValue & "'," & _
                     " '" & ddlEstabelecimento.SelectedValue & "'," & _
                     " '" & ddlFornecedorServico.SelectedValue & "'," & _
                     " '" & txtNumeroDocumento.Text & "'," & _
                     " " & strDataDocumento & "," & _
                     " " & strDataLancamento & "," & _
                     " '" & ddlDI.Text & "'," & _
                     " " & strDataRegistro & "," & _
                     " '" & ddlMoeda.SelectedValue & "'," & _
                     " '" & Session("sNM_USERNICKNAME").ToString & "'," & _
                     " " & nLastUpdated & "" & _
                     " );"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString
            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()
                theSqlDataReader.Close()
                AddRecord = True
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Function

    '' Step 14
    Private Function EditRecord(ByVal strConn As String) As Boolean

        Dim strDataDocumento As String
        Dim strDataLancamento As String
        Dim strDataRegistro As String
        Dim nDiaCreated As String
        Dim nMesCreated As String
        Dim nAnoCreated As String
        Dim nLastUpdated As String


        Dim strVlTotDespesas As String
        Dim strVlDescontoComercial As String
        Dim strVlDebitos As String
        Dim strVlOutrosCreditos As String
        Dim strVlTaxaTSA As String
        Dim strVlTaxaFTI As String
        Dim strVlBCICMS As String
        Dim strVLICMS As String
        Dim strSISCOMEX As String

        Dim strVlDescontoComercialUSD As String
        Dim strVlDebitosUSD As String
        Dim strVlOutrosCreditosUSD As String
        Dim strVlTaxaTSAUSD As String
        Dim strVlTaxaFTIUSD As String
        Dim strSISCOMEXUSD As String

        If Not IsNumeric(txtVlTotDespesas.Text) = True Then
            strVlTotDespesas = "Null"
            strVlDescontoComercialUSD = "Null"
        Else
            strVlDescontoComercialUSD = Round(CDbl(txtVlDescontoComercial.Text / lblTaxaConversao.Text), 2)
            strVlDescontoComercialUSD = Replace(strVlDescontoComercialUSD, ",", ".")

            strVlTotDespesas = Replace(txtVlTotDespesas.Text, ",", ".")
        End If

        If Not IsNumeric(txtVlDescontoComercial.Text) = True Then
            strVlDescontoComercial = "Null"
        Else
            strVlDescontoComercial = Replace(txtVlDescontoComercial.Text, ",", ".")
        End If

        If Not IsNumeric(txtVlDebitos.Text) = True Then
            strVlDebitos = "Null"
            strVlDebitosUSD = "Null"
        Else
            strVlDebitosUSD = Round(CDbl(txtVlDebitos.Text / lblTaxaConversao.Text), 2)
            strVlDebitosUSD = Replace(strVlDebitosUSD, ",", ".")

            strVlDebitos = Replace(txtVlDebitos.Text, ",", ".")
        End If

        If Not IsNumeric(txtVlOutrosCreditos.Text) = True Then
            strVlOutrosCreditos = "Null"
            strVlOutrosCreditosUSD = "Null"
        Else
            strVlOutrosCreditosUSD = Round(CDbl(txtVlOutrosCreditos.Text / lblTaxaConversao.Text), 2)
            strVlOutrosCreditosUSD = Replace(strVlOutrosCreditosUSD, ",", ".")

            strVlOutrosCreditos = Replace(txtVlOutrosCreditos.Text, ",", ".")
        End If

        If Not IsNumeric(txtVlTaxaTSA.Text) = True Then
            strVlTaxaTSA = "Null"
            strVlTaxaTSAUSD = "Null"
        Else
            strVlTaxaTSAUSD = Round(CDbl(txtVlTaxaTSA.Text / lblTaxaConversao.Text), 2)
            strVlTaxaTSAUSD = Replace(strVlTaxaTSAUSD, ",", ".")

            strVlTaxaTSA = Replace(txtVlTaxaTSA.Text, ",", ".")
        End If

        If Not IsNumeric(txtVlTaxaFTI.Text) = True Then
            strVlTaxaFTI = "Null"
            strVlTaxaFTIUSD = "Null"
        Else
            strVlTaxaFTIUSD = Round(CDbl(txtVlTaxaFTI.Text / lblTaxaConversao.Text), 2)
            strVlTaxaFTIUSD = Replace(strVlTaxaFTIUSD, ",", ".")

            strVlTaxaFTI = Replace(txtVlTaxaFTI.Text, ",", ".")
        End If

        If Not IsNumeric(txtBCICMS.Text) = True Then
            strVlBCICMS = "Null"
        Else
            strVlBCICMS = Replace(txtBCICMS.Text, ",", ".")
        End If

        If Not IsNumeric(txtVLICMS.Text) = True Then
            strVLICMS = "Null"
        Else
            strVLICMS = Replace(txtVLICMS.Text, ",", ".")
        End If

        If Not IsNumeric(txtSISCOMEX.Text) = True Then
            strSISCOMEX = "Null"
            strSISCOMEXUSD = "Null"
        Else
            strSISCOMEXUSD = Round(CDbl(txtSISCOMEX.Text / lblTaxaConversao.Text), 2)
            strSISCOMEXUSD = Replace(strSISCOMEXUSD, ",", ".")

            strSISCOMEX = Replace(txtSISCOMEX.Text, ",", ".")
        End If


        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            'Dim dAnoVigente As String
            'Dim i As Long

            Dim theQueryString As String = "SELECT CONVERT(CHAR(10), GETDATE(), 103) AS DATADOSERVIDOR"
            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString
            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()
                Do While theSqlDataReader.Read()
                    Session("dDTAno") = Trim(theSqlDataReader(0))
                Loop
                theSqlDataReader.Close()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try

        End Using

        nDiaCreated = Mid(Session("dDTAno"), 1, 2)
        nMesCreated = Mid(Session("dDTAno"), 4, 2)
        nAnoCreated = Mid(Session("dDTAno"), 7, 4)
        nLastUpdated = nDiaCreated & "/" & nMesCreated & "/" & nAnoCreated

        If (IsDate(nLastUpdated)) Then
            nLastUpdated = "'" & FormatMediumDate(nLastUpdated) & "'"
        Else
            nLastUpdated = "Null"
        End If

        EditRecord = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)


            strDataDocumento = "'" & FormatMediumDate(txtDataDocumento.Text) & "'"
            strDataLancamento = "'" & FormatMediumDate(txtDataLancamento.Text) & "'"
            strDataRegistro = "'" & FormatMediumDate(txtDataRegistro.Text) & "'"


            Dim theQueryString As String
            theQueryString = "UPDATE custoimportacao SET" & _
                     " CodTipoTributacaoII = " & ddlTipoTributacaoII.SelectedValue & "," & _
                     " CodTipoTributacaoIPI = " & ddlTipoTributacaoIPI.SelectedValue & "," & _
                     " CodTipoTributacaoPIS = " & ddlTipoTributacaoPIS.SelectedValue & "," & _
                     " CodTipoTributacaoCOFINS = " & ddlTipoTributacaoCOFINS.SelectedValue & "," & _
                     " CodTipoTributacaoICMS = " & ddlTipoTributacaoICMS.SelectedValue & "," & _
                     " CodTipoTributacaoICMSST = " & ddlTipoTributacaoICMSST.SelectedValue & "," & _
                     " VlTotDespesas = " & strVlTotDespesas & "," & _
                     " VlDescontoComercial = " & strVlDescontoComercial & "," & _
                     " VlDebitos = " & strVlDebitos & "," & _
                     " VlOutrosCreditos = " & strVlOutrosCreditos & "," & _
                     " VlTaxaTSA = " & strVlTaxaTSA & "," & _
                     " VlTaxaFTI = " & strVlTaxaFTI & "," & _
                     " VlBCICMS = " & strVlBCICMS & "," & _
                     " VLICMS = " & strVLICMS & "," & _
                     " SISCOMEX = " & strSISCOMEX & "," & _
                     " VlDescontoComercialUSD = " & strVlDescontoComercialUSD & "," & _
                     " VlDebitosUSD = " & strVlDebitosUSD & "," & _
                     " VlOutrosCreditosUSD = " & strVlOutrosCreditosUSD & "," & _
                     " VlTaxaTSAUSD = " & strVlTaxaTSAUSD & "," & _
                     " VlTaxaFTIUSD = " & strVlTaxaFTIUSD & "," & _
                     " SISCOMEXUSD = " & strSISCOMEXUSD & ","

            theQueryString = theQueryString & _
                     " LastUser = '" & Session("sNM_USERNICKNAME").ToString & "'," & _
                     " LastUpdated = " & nLastUpdated & ""

            theQueryString = theQueryString & _
                     " WHERE CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
                     " AND CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'" & _
                     " AND CodigoDoEstabelecimento = '" & ddlEstabelecimento.SelectedValue & "'" & _
                     " AND CodFornecedorServico = '" & ddlFornecedorServico.SelectedValue & "'" & _
                     " AND NumeroDocumento = '" & UCase(Trim(txtNumeroDocumento.Text)) & "';"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString
            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()
                theSqlDataReader.Close()
                EditRecord = True
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Function

    '' Step 15
    Private Function VerifyData(ByVal strConn As String) As Boolean

        VerifyData = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim theQueryString As String
            theQueryString = "SELECT NumeroDocumento" & _
                         " FROM custoimportacao" & _
                         " WHERE CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
                         " AND CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'" & _
                         " AND CodigoDoEstabelecimento = '" & ddlEstabelecimento.SelectedValue & "'" & _
                         " AND CodFornecedorServico = '" & ddlFornecedorServico.SelectedValue & "'" & _
                         " AND NumeroDocumento = '" & UCase(Trim(txtNumeroDocumento.Text)) & "';"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                If (theSqlDataReader.Read()) Then
                    VerifyData = True
                    lblMsg.Text = "Exclusão não permitida, existe relação de Despesas com Linhas."
                End If

                theSqlDataReader.Close()
            Catch ex As Exception
                VerifyData = True
                'theSqlDataReader.Connection.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Function

    '' Step 16
    Private Function DeleteRecord(ByVal strConn As String) As Boolean

        DeleteRecord = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim theQueryString As String
            theQueryString = "DELETE FROM custoimportacao" & _
                         " WHERE CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
                         " AND CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'" & _
                         " AND CodigoDoEstabelecimento = '" & ddlEstabelecimento.SelectedValue & "'" & _
                         " AND CodFornecedorServico = '" & ddlFornecedorServico.SelectedValue & "'" & _
                         " AND NumeroDocumento = '" & UCase(Trim(txtNumeroDocumento.Text)) & "';"
            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString
            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()
                theSqlDataReader.Close()
                DeleteRecord = True
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Function

    '' Step 17
    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click

        Response.Redirect("CustoDeImportacao.aspx")

    End Sub

    Protected Sub ddlEstabelecimento_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlEstabelecimento.SelectedIndexChanged

        lblMsg.Text = ""

        LoadUFDestino()

        'FillDDLDI()

    End Sub

    Protected Sub ddlFornecedorServico_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlFornecedorServico.SelectedIndexChanged

        lblMsg.Text = ""

        LoadUFOrigem()

    End Sub

    Public Function FormatMediumDate(ByVal DateValue As String) As String

        Dim strYYYY
        Dim strMM
        Dim strDD

        'Strip the Year
        strYYYY = CStr(DatePart("yyyy", DateValue))

        'Strip the Month	
        strMM = CStr(DatePart("m", DateValue))

        'See if month is a single digit if so display it as 01, 02, 03, etc
        If Len(strMM) = 1 Then strMM = "0" & strMM

        'Strip the Date	
        strDD = CStr(DatePart("d", DateValue))

        'See if the Day is a single digit day if so diaply it as 01, 02, 03, etc
        If Len(strDD) = 1 Then strDD = "0" & strDD

        'return the date formatted as DD/MM/YYY	
        FormatMediumDate = strYYYY & "/" & strMM & "/" & strDD

        'you could return the date many ways just retype the above string
        'FormatMediumDate = strDD & "-" & strMM & "-" & strYYYY
        'FormatMediumDate = strMM & "/" & strDD & "/" & strYYYY
        'FormatMediumDate = strYYYY & "-" & strMM & "-" & strMM

    End Function


    Protected Sub btnContinuar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnContinuar.Click

        Session.Add("strOperacao", "A") ' Vazio

        Session.Add("strHolding", lblHolding.Text)
        Session.Add("strCodigoDaHolding", lblHolding.ToolTip)
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(ddlCompanhia.SelectedValue))
        Session.Add("strCodigoDoEstabelecimento", HttpUtility.HtmlDecode(ddlEstabelecimento.SelectedValue))
        Session.Add("strCodFornecedorServico", HttpUtility.HtmlDecode(ddlFornecedorServico.SelectedValue))
        Session.Add("strNumeroDocumento", HttpUtility.HtmlDecode(txtNumeroDocumento.Text))
        Session.Add("strDataDocumento", HttpUtility.HtmlDecode(txtDataDocumento.Text))
        Session.Add("strDI", HttpUtility.HtmlDecode(ddlDI.Text))
        Session.Add("strDataRegistro", HttpUtility.HtmlDecode(txtDataRegistro.Text))
        Session.Add("strMoeda", HttpUtility.HtmlDecode(ddlMoeda.SelectedValue))


        Response.Redirect("CustoDeImportacaoDespAduaneirasL.aspx")

    End Sub


    Protected Sub txtVlTaxaTSA_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtVlTaxaTSA.TextChanged


        If IsNumeric(txtVlTaxaTSA.Text) = True Then
            If IsNumeric(txtVlTotDespesas.Text) = True Then
                txtBCICMS.Text = Round(CDbl((+txtVlTotDespesas.Text + txtVlTaxaTSA.Text) / 0.82999999999999996), 2)
                If (ddlTipoTributacaoICMS.SelectedValue = "1") Then
                    txtVLICMS.Text = Round(CDbl((+txtBCICMS.Text) * 0.17000000000000001), 2)
                Else
                    txtVLICMS.Text = 0
                End If
            End If
        Else
            txtVLICMS.Text = ""
        End If

    End Sub


End Class
